package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.webSocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "订单接口")
@Slf4j
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WebSocketServer webSocketServer;
    @Autowired
    private OrderMapper orderMapper;


    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> UserToOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = new OrderSubmitVO();
        orderSubmitVO = orderService.OrderSubmit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }


    /**
     * 用户支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("用户支付")
    public Result<String> pay(@RequestBody OrdersPaymentDTO ordersPaymentDTO) {
        log.info("订单支付{}", ordersPaymentDTO);
        String time = orderService.pay(ordersPaymentDTO);
        return Result.success(time);
    }


    /**
     * 历史订单查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation("历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> OrderHistorySearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("历史订单{}", ordersPageQueryDTO);
        PageResult pageResult = new PageResult();
        ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
        BaseContext.removeCurrentId();
        pageResult.setTotal(orderService.CountOrders(ordersPageQueryDTO));
        pageResult.setRecords(orderService.getOrders(ordersPageQueryDTO));
        return Result.success(pageResult);
    }

    /**
     * 历史订单查询
     *
     * @param page
     * @param pageSize
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
//    @GetMapping("/historyOrders")
//    @ApiOperation("历史订单查询")
//    public Result<PageResult> page(int page, int pageSize, Integer status) {
//        PageResult pageResult = orderService.pageQuery4User(page, pageSize, status);
//        log.info("最终返回结果{}",pageResult);
//        return Result.success(pageResult);
//    }


    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable Long id) {
        log.info("查询订单详情{}", id);
        OrderVO orderVO = new OrderVO();
        orderVO = orderService.getOrderDetailById(id);
        return Result.success(orderVO);
    }


    /**
     * 取消订单
     *
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Integer id) {
        log.info("取消订单{}id", id);
        orderService.cancel(id);
        return Result.success();
    }

    /**
     * 再来一单
     *
     * @param id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Integer id) {
        log.info("再来一单{}订单id", id);
        orderService.repetition(id);
        return Result.success();
    }


    /**
     * 催单
     *
     * @param id
     * @return
     */
    @ApiOperation("催单")
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Integer id) {
        log.info("用户催单{}", id);
        //websocket发送催单提示
        Map<String, Object> map = new HashMap<>();
        map.put("type", 2);//1来单提示
        map.put("orderId", id);
        Orders orders = new Orders();
        orders.setId(Long.valueOf(id));
        map.put("content", "订单号:" + orderMapper.getByIdToNumber(orders));
        String jsonString = JSON.toJSONString(map);
        webSocketServer.sendToAllClient(jsonString);
        return Result.success();
    }
}
